package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.OlderMapper;
import cn.tedu.carehome.foundation.mapper.OtherMapper;
import cn.tedu.carehome.foundation.mapper.OutgoingMapper;
import cn.tedu.carehome.foundation.pojo.dto.OutgoingAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.OutgoingUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.Event;
import cn.tedu.carehome.foundation.pojo.entity.Outgoing;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.service.IOutgoingService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OutgoingServiceImpl implements IOutgoingService {
    @Autowired
    private OutgoingMapper outgoingMapper;

    @Autowired
    private OtherMapper otherMapper;


    @Autowired
    private OlderMapper olderMapper;


    @Override
    public void addNewplus(OutgoingAddNewDTO outgoingAddNewDTO){


        log.debug("开始处理【新增外出申请】的业务，参数：{}", outgoingAddNewDTO);
        //检查老人是否存在
        OlderStandardVO olderStandardVO=olderMapper.getStandardById(outgoingAddNewDTO.getOlderId());
        if (olderStandardVO==null){
            String message="错误!老人不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
//
//        //检查老人有没有绑定员工
//        StaffStandardVO staffStandardVO= otherMapper.getStaffByOlderIdReal(outgoingAddNewDTO.getOlderId());
//        if (staffStandardVO==null){
//            String message="错误!该老人没有绑定护工!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
//        }
        //检查该老人有没有正在申请的外出申请
        log.debug("开始检查该老人有没有正在申请的外出申请");
        List<OutgoingStandardVO> list=outgoingMapper.getOutgingByOlderId(outgoingAddNewDTO.getOlderId());
        log.debug("检测结果:{}",list);
        for(OutgoingStandardVO item:list){
            if(item.getCheckState()==0){
                String message="错误!该老人已经存在正在审核的外出申请!";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }

        }
        Outgoing outgoing=new Outgoing();
        BeanUtils.copyProperties(outgoingAddNewDTO,outgoing);
        //将老人对应的护工id和默认审核状态传给新的outgoing对象

        outgoing.setCheckState(0);

        //设置审核人为副院长,
        StaffStandardVO vicePresidentOne=otherMapper.getVicePresidentOne();

//        outgoing.setCheckStaffId(vicePresidentOne.getId());
        String checkResult="审核中...";
        outgoing.setCheckResult(checkResult);
        log.debug("即将插入数据，参数：{}", outgoing);
        int rows = outgoingMapper.insertplus(outgoing);
        if (rows != 1) {
            String message = "删除失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }



    @Override
    public void addNew(OutgoingAddNewDTO outgoingAddNewDTO){


        log.debug("开始处理【新增外出申请】的业务，参数：{}", outgoingAddNewDTO);
        //检查老人是否存在
        OlderStandardVO olderStandardVO=olderMapper.getStandardById(outgoingAddNewDTO.getOlderId());
        if (olderStandardVO==null){
            String message="错误!老人不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
//
//        //检查老人有没有绑定员工
//        StaffStandardVO staffStandardVO= otherMapper.getStaffByOlderIdReal(outgoingAddNewDTO.getOlderId());
//        if (staffStandardVO==null){
//            String message="错误!该老人没有绑定护工!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
//        }
        //检查该老人有没有正在申请的外出申请
        log.debug("开始检查该老人有没有正在申请的外出申请");
        List<OutgoingStandardVO> list=outgoingMapper.getOutgingByOlderId(outgoingAddNewDTO.getOlderId());
        log.debug("检测结果:{}",list);
        for(OutgoingStandardVO item:list){
            if(item.getCheckState()==0){
                String message="错误!该老人已经存在正在审核的外出申请!";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }

        }
        Outgoing outgoing=new Outgoing();
        BeanUtils.copyProperties(outgoingAddNewDTO,outgoing);
        //将老人对应的护工id和默认审核状态传给新的outgoing对象

        outgoing.setCheckState(0);

        //设置审核人为副院长,
        StaffStandardVO vicePresidentOne=otherMapper.getVicePresidentOne();

//        outgoing.setCheckStaffId(vicePresidentOne.getId());
        String checkResult="审核中...";
        outgoing.setCheckResult(checkResult);
        log.debug("即将插入数据，参数：{}", outgoing);
        int rows = outgoingMapper.insert(outgoing);
        if (rows != 1) {
            String message = "删除失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void deleteByIdplus(Long id) {
        log.debug("开始处理【根据id删除外出申请】的业务，编号参数：{}", id);
        //检查是否存在
//        OutgoingStandardVO outgoingStandardVO= outgoingMapper.getStandardById(id);
//        if (outgoingStandardVO==null){
//            String message="删除失败,申请不存在!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
//        }
        //只有驳回和通过的申请可以删除
//        if(outgoingStandardVO.getCheckState()==0){
//            String message="删除失败,无法删除审核中的外出申请!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
//        }
        int rows = outgoingMapper.deleteByIdplus(id);
        if (rows != 1) {
            String message = "删除失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }




    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除外出申请】的业务，编号参数：{}", id);
        //检查是否存在
        OutgoingStandardVO outgoingStandardVO= outgoingMapper.getStandardById(id);
        if (outgoingStandardVO==null){
            String message="删除失败,申请不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //只有驳回和通过的申请可以删除
//        if(outgoingStandardVO.getCheckState()==0){
//            String message="删除失败,无法删除审核中的外出申请!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
//        }
        int rows = outgoingMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public OutgoingStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询外出申请详情】的业务，无参数");
        //检查id不存在
        OutgoingStandardVO outgoingStandardVO=outgoingMapper.getStandardById(id);
        if (outgoingStandardVO==null){
            String message="错误!id不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return outgoingStandardVO;
    }

    @Override
    public List<OutgoingListItemVO> list() {
        log.debug("开始处理【查询外出申请列表】的业务，无参数");
        List<OutgoingListItemVO> list=outgoingMapper.list();
        log.debug("即将返回查询结果：{}", list);
        return list;
    }

    @Override
    public List<OutgoingListItemVO> listplus() {
        log.debug("开始处理【查询外出申请列表】的业务，无参数");
        List<OutgoingListItemVO> list=outgoingMapper.listplus();
        log.debug("即将返回查询结果：{}", list);
        return list;
    }



    @Override
    public void setPass(Long id, String checkResult) {
        updateInfoByIdPlus(id,checkResult);
    }

    @Override
    public void setReject(Long id, String checkResult) {
        updateInfoById(id,2,checkResult);

    }


    public void updateInfoByIdPlus(Long id,String shouldbackdate) {
//        String stateText[] = {"审核中","审核通过", "审核驳回"};
//        log.debug("开始处理【使得外出申请{}】的业务，ID：{}，目标状态：{}", stateText[checkState], id, stateText);
        //查询是否存在
        OutgoingStandardVO outgoingStandardVO= outgoingMapper.getStandardById(id);
//        if (outgoingStandardVO==null){
//            String message=stateText[checkState]+"失败,申请不存在!";
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
//        }
//
//        // 判断以上查询结果中的state是否与参数state相同
//        if (outgoingStandardVO.getCheckState()==checkState){
//            String message=stateText[checkState]+"失败,申请已经"+stateText[checkState];
//            log.debug(message);
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
//        }

        //判断state是否符合逻辑(state0->state1,state0->state2)
//        if (checkState-outgoingStandardVO.getCheckState()>2){
//            String messege="该事件"+stateText[outgoingStandardVO.getCheckState()]+",无法改为"+stateText[checkState]+"状态";
//            log.debug(messege);
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,messege);
//        }

        // 创建Event对象，并将2个参数的值封装进来
        OutgoingUpdateDTO outgoingUpdateDTO=new OutgoingUpdateDTO();
        outgoingUpdateDTO.setId(id);
        outgoingUpdateDTO.setShouldbackdate(shouldbackdate);
        int rows = outgoingMapper.update(outgoingUpdateDTO);
        if (rows != 1) {
            String message ="修改失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    public void updateInfoById(Long id, Integer checkState,String checkResult) {
        String stateText[] = {"审核中","审核通过", "审核驳回"};
        log.debug("开始处理【使得外出申请{}】的业务，ID：{}，目标状态：{}", stateText[checkState], id, stateText);
        //查询是否存在
        OutgoingStandardVO outgoingStandardVO= outgoingMapper.getStandardById(id);
        if (outgoingStandardVO==null){
            String message=stateText[checkState]+"失败,申请不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        // 判断以上查询结果中的state是否与参数state相同
        if (outgoingStandardVO.getCheckState()==checkState){
            String message=stateText[checkState]+"失败,申请已经"+stateText[checkState];
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //判断state是否符合逻辑(state0->state1,state0->state2)
        if (checkState-outgoingStandardVO.getCheckState()>2){
            String messege="该事件"+stateText[outgoingStandardVO.getCheckState()]+",无法改为"+stateText[checkState]+"状态";
            log.debug(messege);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,messege);
        }

        // 创建Event对象，并将2个参数的值封装进来
        OutgoingUpdateDTO outgoingUpdateDTO=new OutgoingUpdateDTO();
        outgoingUpdateDTO.setId(id);
        outgoingUpdateDTO.setCheckState(checkState);
        outgoingUpdateDTO.setCheckResult(checkResult);
        int rows = outgoingMapper.update(outgoingUpdateDTO);
        if (rows != 1) {
            String message = stateText[checkState] + "修改失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }




}
